<?
include_once("Warmy_GD.class.php");
include_once("Warmy_GB2312.class.php");
class WImage extends Warmy_GD
{
    private $Watermark;
	//源图象

    private $srcimg;
    //临时创建的图象

    private $im;
	//源图
	private $Source			= array(
							"type"		=>"2",
							"width"		=>0,
							"height"	=>0);
	private $Resize_para	= array(
							"width"		=>0,
							"height"	=>0,
							"cut"		=>0);
	private $Water_para		= array(
							"Type"				=> 1,
							"text"				=> "www.warmy.cn",
							"FontColor"			=> "FF0000",
							"FontShadowColor"	=> "ffffff",
							"ShadowOffset"		=> 1,
							"FontSize"			=> 9,
							"FontFamily"		=> "ARIAL.TTF",
							"Image"				=> "",
							"OffsetX"			=> 10,
							"OffsetY"			=> 10);
	function WImage($Watermark = 0,$Water_para = array())
	{
		$this->Watermark				= $Watermark == 0 ? 0 : 1;
		if($this->Watermark)
			$this->Initialize_Water_para($Water_para);
	}
	function __set($key, $value)
	{
		if($key == "Water_para")
			$this->Initialize_Water_para($value);
		else 
			$this->$key					= $value;
	}
	function Initialize_Water_para($Water_para)
	{
		if(isset($Water_para["Type"]) && ereg("^[012]{1}$",$Water_para["Type"]))
			$this->Water_para["Type"]			= $Water_para["Type"];
		else
			$this->Water_para["Type"]			= 1;
		
		if($this->Water_para["Type"] == 1)
		{
			if(isset($Water_para["text"]))
			{
				$GB2UTF8 = new GB2UTF8($Water_para["text"]);
				$this->Water_para["text"]			= $GB2UTF8->utf8;
			}
		}
		elseif($this->Water_para["Type"] == 2)
		{
			if(isset($Water_para["Image"]) && $Water_para["Image"] != "")
			{
				if(file_exists($Water_para["Image"]))
					$this->Water_para["Image"]			=	 $Water_para["Image"];
				else
					$this->halt("水印图片 {$Water_para["Image"]} 不存在");
			}
			else
				$this->Water_para["Image"]	= dirname(__FILE__) . "\\warmy.gif";
			
		}
		if(isset($Water_para["FontColor"]))
			$this->Water_para["FontColor"]		= $this->Check_Color($Water_para["FontColor"]);
		if(isset($Water_para["FontShadowColor"]))
			$this->Water_para["FontShadowColor"]= $this->Check_Color($Water_para["FontShadowColor"]);
		if(isset($Water_para["ShadowOffset"]))
			$this->Water_para["ShadowOffset"]			= $Water_para["ShadowOffset"];
		if(isset($Water_para["FontSize"]))
			$this->Water_para["FontSize"]		= $Water_para["FontSize"];
		if(isset($Water_para["FontFamily"]))
			$this->Water_para["FontFamily"]		= $Water_para["FontFamily"];
		
		if(isset($Water_para["OffsetX"]))
			$this->Water_para["OffsetX"]		=	 $Water_para["OffsetX"];
		if(isset($Water_para["OffsetY"]))
			$this->Water_para["OffsetY"]		=	 $Water_para["OffsetY"];

	}
	function Water($img)
	{

		$this->srcimg					= $img;
		if(!file_exists($this->srcimg))
		{
			$this->halt("{$this->srcimg} 图片不存在！");
		}
		
        $this->Source["type"]			= substr(strrchr($this->srcimg,"."),1);
        $this->Check_Image_Type();
		$this->Source["width"]			= imagesx($this->im);
        $this->Source["height"]			= imagesy($this->im);
		if(!imagealphablending($this->im, true))
			$this->halt("设定图像的混色模式失败！");
		if($this->Watermark)
			$this->Mark();

	}
    function ResizeImage($img, $wid, $hei = 0,$c = 0)
    {
        if(isset($img) && $img != "")
        {
	    	if(!file_exists($img))
	        	$this->halt("$img 图片不存在！");
	        $this->srcimg				= $img;
        }
        elseif($this->srcimg == "" || !file_exists($this->srcimg)) 
        	$this->halt("没有设置源图");
    	
		$this->Resize_para["width"]		= $wid;
		$this->Resize_para["height"]	= $hei;
		$this->Resize_para["cut"]		= $c;
        $this->Source["type"]			= substr(strrchr($this->srcimg,"."),1);
        $this->Check_Image_Type();
        $this->Source["width"]			= imagesx($this->im);
        $this->Source["height"]			= imagesy($this->im);
        if($hei == 0)
        	$this->Resize_para["height"]=ceil($wid/$this->Source["width"]*$this->Source["height"]);
		$this->Resize();
    }
    function Resize()
    {
        //改变后的图象的比例

        $resize_ratio = ($this->Resize_para["width"])/($this->Resize_para["height"]);
        //实际图象的比例

        $ratio = ($this->Source["width"])/($this->Source["height"]);
        if(($this->Resize_para["cut"])=="1")
        //裁图
        {
            if($ratio>=$resize_ratio)
            //高度优先
            {
                $newimg = imagecreatetruecolor($this->Resize_para["width"],$this->Resize_para["height"]);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->Resize_para["width"],$this->Resize_para["height"], (($this->Source["height"])*$resize_ratio), $this->Source["height"]);
            }
            if($ratio<$resize_ratio)
            //宽度优先
            {
                $newimg = imagecreatetruecolor($this->Resize_para["width"],$this->Resize_para["height"]);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->Resize_para["width"], $this->Resize_para["height"], $this->Source["width"], (($this->Source["width"])/$resize_ratio));
            }
        }
        elseif($this->Resize_para["cut"]=="2")
		//压缩
		{
			$newimg = imagecreatetruecolor($this->Resize_para["width"],($this->Resize_para["height"]));
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->Resize_para["width"], ($this->Resize_para["height"]), $this->Source["width"], $this->Source["height"]);
		}
		else
        //不裁图

        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->Resize_para["width"],($this->Resize_para["width"])/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->Resize_para["width"], ($this->Resize_para["width"])/$ratio, $this->Source["width"], $this->Source["height"]);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->Resize_para["height"])*$ratio,$this->Resize_para["height"]);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->Resize_para["height"])*$ratio, $this->Resize_para["height"], $this->Source["width"], $this->Source["height"]);
            }
        }
		$this->im = $newimg;
		if($this->Watermark)
		{
			if(!imagealphablending($this->im, true))
				$this->halt("设定图像的混色模式失败！");
			$this->Mark();
		}
		
    }
	function Save($path)
	{
		//if(substr($path,0,1) == "/" || substr($path,0,1) == "\\")
		//	$path	= "{$_SERVER["DOCUMENT_ROOT"]}$path";
		ImageJpeg ($this->im,"$path.{$this->Source["type"]}");

	}
    //初始化图象

    function Check_Image_Type()
    {
        if($this->Source["type"]=="jpg")
            $this->im = imagecreatefromjpeg($this->srcimg);
		elseif($this->Source["type"]=="gif")
            $this->im = imagecreatefromgif($this->srcimg);
        elseif($this->Source["type"]=="png")
            $this->im = imagecreatefrompng($this->srcimg);
		else
			$this->halt("系统不能分辨{this->type}格式图片");
    }   
	function __destruct()
	{
		if(isset($this->im))
			ImageDestroy ($this->im);
	}
	function Mark()
	{
		if($this->Water_para["Type"] == 2)
		{
			$water_info = getimagesize($this->Water_para["Image"] ); 
			$water_w = $water_info[0];
			$water_h = $water_info[1];
			$water_im = imagecreatefromgif($this->Water_para["Image"]);
			imagecopy($this->im,$water_im , 0, 0, 0, 0, $water_w, $water_h);
		}
		elseif($this->Water_para["Type"] == 1)
		{
			$R = hexdec(substr($this->Water_para["FontShadowColor"],0,2));
			$G = hexdec(substr($this->Water_para["FontShadowColor"],2,2));
			$B = hexdec(substr($this->Water_para["FontShadowColor"],4));
			$textcolor		= imagecolorallocate($this->im, $R, $G, $B);
			$n_SYHeight		= 0;
			if($this->Water_para["FontFamily"])
			{
				$fontSize		= imagettfbbox($this->Water_para["FontSize"], 0, $this->Water_para["FontFamily"], $this->Water_para["text"]);
				$n_SYHeight		= $fontSize[1] - $fontSize[7];
				imagettftext($this->im, $this->Water_para["FontSize"], 0, $this->Water_para["OffsetX"]+$this->Water_para["ShadowOffset"], $this->Water_para["OffsetY"]+$n_SYHeight+$this->Water_para["ShadowOffset"], $textcolor, $this->Water_para["FontFamily"],  $this->Water_para["text"]);
			}
			else
			{
				imagestring($this->im, $this->Water_para["FontSize"], $this->Water_para["OffsetX"]+$this->Water_para["ShadowOffset"], $this->Water_para["OffsetY"]+$this->Water_para["ShadowOffset"], $this->Water_para["text"], $textcolor);
			}
			$R = hexdec(substr($this->Water_para["FontColor"],0,2));
			//print($R);
			$G = hexdec(substr($this->Water_para["FontColor"],2,2));
			$B = hexdec(substr($this->Water_para["FontColor"],4));
			$textcolor		= imagecolorallocate($this->im, $R, $G, $B);
			if($this->Water_para["FontFamily"])
			{
				imagettftext($this->im, $this->Water_para["FontSize"], 0, $this->Water_para["OffsetX"], $this->Water_para["OffsetY"]+$n_SYHeight, $textcolor, $this->Water_para["FontFamily"],  $this->Water_para["text"]);
			}
			else
				imagestring($this->im, $this->Water_para["FontSize"], $this->Water_para["OffsetX"], $this->Water_para["OffsetY"], $this->Water_para["text"], $textcolor);
			unset($textcolor);

		}

	}
}
?>